package com.iversonx.adapter.example_1.service.v2;

import com.iversonx.adapter.example_1.remote.db.LogDbOperateApi;
import com.iversonx.adapter.example_1.remote.file.LogFileOperateApi;
import com.iversonx.adapter.example_1.domain.LogModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

public class LogFileAdapter implements LogAdapter {
    
    private final LogFileOperateApi apdatee;
    public LogFileAdapter(LogFileOperateApi apdatee) {
        this.apdatee = apdatee;
    }

    @Override
    public void insertLog(LogModel log) {
        // 模拟从文件中读取内容
        Map<String, LogModel> data = apdatee.readLog();
        // 加入新的日志对象
        data.put(log.getLogId(), log);
        // 重新写入文件
        apdatee.writeLog(data);
    }

    @Override
    public Map<String, LogModel> mapLog() {
        return apdatee.readLog();
    }

    @Override
    public LogModel get(String logId) {
        Map<String, LogModel> data = apdatee.readLog();
        return data.get(logId);
    }
}
